#ifndef _AX58100_HW_H_
#define _AX58100_HW_H_


/*-----------------------------------------------------------------------------------------
------    Includes
-----------------------------------------------------------------------------------------*/
#include "esc.h"
#include "main.h"


#define ESC_RD                    0x02 /**< \brief Indicates a read access to ESC or EEPROM*/
#define ESC_WR                    0x04 /**< \brief Indicates a write access to ESC or EEPROM.*/

#define _STM32_IO8 1

/*---------------------------------------------
-    hardware timer settings
-----------------------------------------------*/

#define ECAT_TIMER_INC_P_MS                2000 //72000   	//0x271 /**< \brief 625 ticks per ms*/



/*---------------------------------------------
-    Interrupt and Timer defines
-----------------------------------------------*/
//////////////////////////////////////////////////////////////////xiugai 20210921

#ifndef DISABLE_ESC_INT
#define    DISABLE_ESC_INT()           NVIC_DisableIRQ(EXTI9_5_IRQn)		// {(_INT1IE)=0;} /**< \brief Disable interrupt source INT1*/
#endif
#ifndef ENABLE_ESC_INT
#define    ENABLE_ESC_INT()           NVIC_EnableIRQ(EXTI9_5_IRQn)			// {(_INT1IE)=1;} /**< \brief Enable interrupt source INT1*/
#endif

#ifndef HW_GetTimer
#define HW_GetTimer()       		TIM4->CNT		//(SysTick->LOAD - 	SysTick->VAL) //			// ((UINT16)(TMR7)) /**< \brief Access to the hardware timer*/
#endif

#ifndef HW_ClearTimer
#define HW_ClearTimer()         ((TIM4->CNT)=0); //(SysTick->VAL = SysTick->LOAD)// //{(TMR7) = 0;} /**< \brief Clear the hardware timer*/
#endif



#define HW_EscReadByte(ByteValue,Address)	HW_EscRead(((MEM_ADDR *)&(ByteValue)),((UINT16)(Address)),1) 
#define HW_EscReadWord(WordValue, Address) HW_EscRead(((MEM_ADDR *)&(WordValue)),((UINT16)(Address)),2) /**< \brief 16Bit ESC read access*/
#define HW_EscReadDWord(DWordValue, Address) HW_EscRead(((MEM_ADDR *)&(DWordValue)),((UINT16)(Address)),4) /**< \brief 32Bit ESC read access*/
#define HW_EscReadMbxMem(pData,Address,Len) HW_EscRead(((MEM_ADDR *)(pData)),((UINT16)(Address)),(Len)) /**< \brief The mailbox data is stored in the local uC memory therefore the default read function is used.*/

#define HW_EscReadByteIsr(ByteValue,Address)	HW_EscReadIsr(((MEM_ADDR *)&(ByteValue)),((UINT16)(Address)),1) 
#define HW_EscReadWordIsr(WordValue, Address) HW_EscReadIsr(((MEM_ADDR *)&(WordValue)),((UINT16)(Address)),2) /**< \brief Interrupt specific 16Bit ESC read access*/
#define HW_EscReadDWordIsr(DWordValue, Address) HW_EscReadIsr(((MEM_ADDR *)&(DWordValue)),((UINT16)(Address)),4) /**< \brief Interrupt specific 32Bit ESC read access*/

#define HW_EscWriteByte(WordValue, Address) HW_EscWrite(((MEM_ADDR *)&(WordValue)),((UINT16)(Address)),1)
#define HW_EscWriteWord(WordValue, Address) HW_EscWrite(((MEM_ADDR *)&(WordValue)),((UINT16)(Address)),2) /**< \brief 16Bit ESC write access*/
#define HW_EscWriteDWord(DWordValue, Address) HW_EscWrite(((MEM_ADDR *)&(DWordValue)),((UINT16)(Address)),4) /**< \brief 32Bit ESC write access*/
#define HW_EscWriteMbxMem(pData,Address,Len) HW_EscWrite(((MEM_ADDR *)(pData)),((UINT16)(Address)),(Len)) /**< \brief The mailbox data is stored in the local uC memory therefore the default write function is used.*/

#define HW_EscWriteWordIsr(WordValue, Address) HW_EscWriteIsr(((MEM_ADDR *)&(WordValue)),((UINT16)(Address)),2) /**< \brief Interrupt specific 16Bit ESC write access*/
#define HW_EscWriteDWordIsr(DWordValue, Address) HW_EscWriteIsr(((MEM_ADDR *)&(DWordValue)),((UINT16)(Address)),4) /**< \brief Interrupt specific 32Bit ESC write access*/




/*-----------------------------------------------------------------------------------------
------
------    Global variables
------
-----------------------------------------------------------------------------------------*/
/* EXPORTED SUBPROGRAM SPECIFICATIONS */

/**
  * @brief  Access to the hardware timer
  */
#ifndef HW_GetTimer
#define HW_GetTimer()          ((UINT16)(HW_TIMETICK_INSTANCE->CNT>>1))
#endif

/**
  * @brief  Clear the hardware timer
  */
#ifndef HW_ClearTimer
#define HW_ClearTimer()        (HW_TIMETICK_INSTANCE->CNT = 0)
#endif

/*-----------------------------------------------------------------------------------------
------
------    Global functions
------
-----------------------------------------------------------------------------------------*/
UINT8 HW_Init(void);
void HW_Release(void);
UINT16 HW_GetALEventRegister(void);

UINT16 HW_GetALEventRegister_Isr(void);

void HW_ResetALEventMask(UINT16 intMask);
void HW_SetALEventMask(UINT16 intMask);
void HW_SetLed(UINT8 RunLed,UINT8 ErrLed);

void HW_EscRead( MEM_ADDR * pData, UINT16 Address, UINT16 Len );
void HW_EscReadIsr( MEM_ADDR *pData, UINT16 Address, UINT16 Len );

void HW_EscWrite( MEM_ADDR *pData, UINT16 Address, UINT16 Len );

void HW_EscWriteIsr( MEM_ADDR *pData, UINT16 Address, UINT16 Len );

void HW_DisableSyncManChannel(UINT8 channel);
void HW_EnableSyncManChannel(UINT8 channel);
TSYNCMAN ESCMEM *HW_GetSyncMan(UINT8 channel);

void TimerIsr(void);
void EcatIsr(void);
void Sync0Isr(void);
void Sync1Isr(void);
/*---------------------------------------------
-    ESC Access macros
-----------------------------------------------*/
// extern HW_SPI_OBJECT      HW_SpiObj[];

#endif //#if _AX58100_HW_H_


